<template>
	<!-- 
	微信立减金
	http://xmly.syhy000.com/xmly6ljj/#/?a=123aaa
-->
	<view class="main-box">
		
		<submit-form :baseImagePath="baseImagePath" :param="props.param" :mobile="mobileComputed"></submit-form>
		<image class="bg" mode="widthFix" :src="baseImagePath + 'bottom-bg.png'">
		</image>
	</view>
<!-- 	<goto-form-fix-btn :baseImagePath="baseImagePath" :targetId="scrollToTargetId"></goto-form-fix-btn>
	<right-fix-box :baseImagePath="baseImagePath"></right-fix-box> -->
</template>

<script setup>
	import VueScrollTo from "vue-scrollto"
	import {
		computed,
		onMounted,
		ref
	} from "vue";
	import httpUtil from "@/utils/httpUtil.js"
	import deviceUtil from "@/utils/deviceUtil.js"
	import userApi from "@/apis/userApi.js"
	import aliApi from "@/apis/aliApi";
	import orderApi from "@/apis/orderApi.js"
	import commonApi from "@/apis/commonApi.js"
	import md5 from "@/utils/md5.js"
	import validParamsUtil from "@/utils/validParamsUtil";
	import logApi from "@/apis/logApi";
	import messageUtil from "@/utils/messageUtil.js"
	import paramApi from "@/apis/paramApi.js"
	import {
		Base64
	} from 'js-base64';
	import xunjieMobileDecode from "../../apis/xunjieMobileDecode";

	const scrollToTargetId = ref('form-box')
	const scrollData = ref(0);

	const baseImagePath = ref('https://h5.sx2b2c.com/images/xima_laya/lijianjin/version6/')

	const props = defineProps(['a', 'param', 'code', 'openid',
		'from_wxpay', 'openidHF', 'mobile',
		'push_id', 'pushid', 'clickid', 'tokenid', 'adId', 'jtcAdRequestId',
		'qcjParamStr', 'phone'
	])

	onMounted(() => {
		// 监听滚动条位置
		// window.addEventListener("scroll", scrollTopMethod, true);
		// setTimeout(() => {
		// 	toTopMethod();
		// }, 1000)

		initData()
	})

	const initData = () => {
		paramApi.initData(props)
		// initParam()
		commonApi.is404()

		if (props.param) {
			userApi.setParam(props.param)
		}

		// userApi.setMobile(null)

		// #ifdef MP
		initMiniData()
		// #endif
		checkOrderStatus()
		commonApi.buriedPoint()
	}

	const mobileComputed = computed(() => {
		if (!props.mobile && !props.phone) {
			return ''
		}
		if (props.mobile) {
			return props.mobile
		}
		if (props.phone) {
			return xunjieMobileDecode.mobileDecode(props.phone)
		}
		return ''
	})

	// const initParam = () => {
	// 	if (props.a) {
	// 		httpUtil.setA(props.a)
	// 	}
	// 	if (props.openid) {
	// 		userApi.setUserId(props.openid)
	// 	}
	// 	if (props.qcjParamStr) {
	// 		userApi.setParam(props.qcjParamStr)
	// 	}
	// 	if (props.jtcAdRequestId) {
	// 		userApi.setParam(props.jtcAdRequestId)
	// 	}
	// 	if (props.param) {
	// 		userApi.setParam(props.param)
	// 	}
	// 	if (props.push_id) {
	// 		userApi.setParam(props.push_id)
	// 	}
	// 	if (props.pushid) {
	// 		userApi.setParam(props.pushid)
	// 	}
	// 	if (props.clickid) {
	// 		userApi.setParam(props.clickid)
	// 	}
	// 	if (props.clickId) {
	// 		userApi.setParam(props.clickId)
	// 	}
	// 	if (props.tokenid) {
	// 		userApi.setParam(props.tokenid)
	// 	}
	// 	if (props.tokenId) {
	// 		userApi.setParam(props.tokenId)
	// 	}
	// 	if (props.adId) {
	// 		userApi.setParam(props.adId)
	// 	}
	// }

	const checkOrderStatus = () => {
		console.log('checkOrderStatus')
		if (userApi.getMobile()) {
			// messageUtil.showLoading('订单确认中')
			let intervalId = setInterval(() => {
				checkOrderStatusHandler()
			}, 1000)

			setTimeout(() => {
				// messageUtil.closeLoading()
				clearInterval(intervalId)
				userApi.removeMobile()
			}, 10000)
		}
	}

	const checkOrderStatusHandler = () => {
		if (!validParamsUtil.validPhone(userApi.getMobile())) {
			logApi.addLog('checkOrderStatusRequest,手机号不正确' + userApi.getMobile())
			return;
		}

		let queryMobileParmas = {
			mobile: userApi.getMobile()
		}
		logApi.addLog('checkOrderStatusRequest,检查订单是否支付，请求：' + JSON.stringify(queryMobileParmas))

		httpUtil.post('/api/xmly/queryMobile', queryMobileParmas, res => {
			logApi.addLog('checkOrderStatusRequest,检查订单是否支付，返回：' + JSON.stringify(res))
			if (res.data.sign == true) {

				// let targetUrl = 'https://h5.2018sx.cn/newlq/#/?current=0&openid=' +
				// 	md5.hex_md5(userApi.getMobile() + '456') +
				// 	'&mobile=' +
				// 	userApi.getMobile() +
				// 	'&openidHF=' + userApi.getUserId();
				// userApi.removeMobile()
				// window.location.href = targetUrl

				window.location.href =
					'https://m.ximalaya.com/applink?open_xm=iting%3A%2F%2Fopen%3Fcopycode%3D1%2520http%253A%252F1%2523Xm001tkH94%2523%2522&from=&keep=true&isSeo=true&fcid=10000&fpage=https%3A%2F%2Fm.ximalaya.com%2F'
			} else {

			}
		})

	}

	const initMiniData = () => {
		userApi.getMiniLoginCode(code => {
			userApi.initUserId(code)
		})
	}
	const scrollTopMethod = () => {
		scrollData.value = document.documentElement.scrollTop || document.body.scrollTop;
	}

	const toTopMethod = () => {
		console.log('toTopMethod', scrollToTargetId.value)
		let offset = document.documentElement.clientHeight / 2 - 100;
		VueScrollTo.scrollTo(document.getElementById(scrollToTargetId.value), 1000, {
			offset: -offset,
		});
	}
</script>

<style lang="scss">
	.main-box {
		position: relative;
		background-color: #F6304C;
		display: none;
	}

	.bg {
		width: 100%;
		display: block;
	}
</style>